package com.example.demo;

import okhttp3.*;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Map;

public class OkHttpClientUtils {

    private static final MediaType JSON_MEDIA_TYPE = MediaType.parse("application/json; charset=utf-8");

    private static final OkHttpClient OKHTTPCLIENT = new OkHttpClient();

    /**
     * 发送get请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static String get(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        Call call = OKHTTPCLIENT.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    /**
     * 发送get请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static String getWithHeader(String url, Map<String, String> header) throws IOException {
        Request.Builder builder = new Request.Builder()
                .url(url)
                .get();

        for (Map.Entry<String, String> entry : header.entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }

        Request request = builder.build();
        Call call = OKHTTPCLIENT.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    /**
     * 发送post请求
     *
     * @param url
     * @param json
     * @return
     * @throws IOException
     */
    public static String post(String url, String json) throws IOException {
        return post(url, RequestBody.create(json, JSON_MEDIA_TYPE));
    }

    /**
     * 发送post请求
     *
     * @param url
     * @param body
     * @return
     * @throws IOException
     */
    private static String post(String url, RequestBody body) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = OKHTTPCLIENT.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    /**
     * 发送post请求
     *
     * @param url
     * @param json
     * @return
     * @throws IOException
     */
    public static String postWithHeader(String url, String json, Map<String, String> header) throws IOException {
        RequestBody body = RequestBody.create(json, JSON_MEDIA_TYPE);
        Request.Builder builder = new Request.Builder()
                .url(url)
                .post(body);

        for (Map.Entry<String, String> entry : header.entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }

        Request request = builder.build();
        Call call = OKHTTPCLIENT.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    public static String postWithHeader(String url, String json, Map<String, String> header, Boolean proxyOnly) throws IOException {
        RequestBody body = RequestBody.create(json, JSON_MEDIA_TYPE);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("YOUR_PROXY_HOST", 8080));

        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().proxy(proxy);
        OkHttpClient OKHTTPCLIENT = clientBuilder.build();

        Request.Builder builder = new Request.Builder()
                .url(url)
                .post(body);

        for (Map.Entry<String, String> entry : header.entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }

        Request request = builder.build();
        Call call = OKHTTPCLIENT.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }


}
